package com.ztgf.logistics.scheduled;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.ztgf.dubbo.exception.ServiceException;
import com.ztgf.logistics.models.dto.deppon.LogisticsBillDTO;
import com.ztgf.logistics.models.service.ILogisticsService;
import com.ztgf.logistics.models.service.deppon.ILogisticsBillService;
import com.ztgf.logistics.models.vo.QueryTrackResp;
import com.ztgf.order.models.service.IOrderInfoService;
import com.ztgf.order.models.service.IOrderStatusService;
import com.ztgf.order.models.service.common.ICommonService;
import com.ztgf.order.models.vo.OrderLogisticsVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

import static com.ztgf.common.utils.UtilConstants.isNull;


/**
 * @Description ：
 * @Author ： 解风
 * @Date ： 2018/01/26 14:10
 */
@Component
@Slf4j
public class ScheduledTimes {

    @DubboReference
    private IOrderStatusService orderStatusService;
    @DubboReference
    private IOrderInfoService orderInfoService;
    @DubboReference
    private ILogisticsService logisticsService;
    @DubboReference
    private ICommonService commonService;

    @DubboReference
    private ILogisticsBillService logisticsBillService;

    /**
     * 暂时 每1分钟执行一次 更新订单过期时间
     */
    @Scheduled(cron = "0 0/1 * * * ?")
    public void executeOrderExcepire() {
        log.info("--------------定时任务：先款订单检测开始 ------------------");
        boolean isSelect = true;
        Long lastNo = 0L;
        do {
            List<OrderLogisticsVO> list = orderInfoService.queryAdvanceOrderList(lastNo);
            log.info("先款订单 : {}", list);
            if (!isNull(list)) {
                if (list.size() < 50) {
                    isSelect = false;
                }
                for (OrderLogisticsVO orderLogisticsVo : list) {
                    lastNo = orderLogisticsVo.getId();
                    // 查询订单物流信息
                    List<String> orderLogistics = logisticsService.queryAdvanceOrderLogistics(orderLogisticsVo.getId());
                    if (isNull(orderLogistics)) {
                        continue;
                    }
                    for (String logistic : orderLogistics) {
                        QueryTrackResp queryTrackResp = JSON.parseObject(logistic, QueryTrackResp.class);
                        if (null != queryTrackResp && "200".equals(queryTrackResp.getStatus()) && queryTrackResp.getState() == 3) {
                            // 订单已签收
                            try {
                                commonService.saveReceive(orderLogisticsVo.getId(), false);
                            } catch (ServiceException e){
                                log.error("物流订单签收异常ServiceException: " + orderLogisticsVo.getId(), e);
                            } catch (RuntimeException e){
                                log.error("物流订单签收异常RuntimeException: " + orderLogisticsVo.getId(), e);
                            } catch (Exception e){
                                log.error("物流订单签收异常Exception: " + orderLogisticsVo.getId(), e);
                            }

                        } else {
                            continue;
                        }
                    }
                }
            } else {
                isSelect = false;
            }
        } while (isSelect);
        log.info("--------------定时任务：先款订单检测结束 ------------------");
    }

    /**
     * 账单逾期 凌晨0点执行
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void executeBillOverdue() {
        log.info("--------------定时任务：逾期账单 ------------------");
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("member_type", 0);
        wrapper.eq("overdue_status", 0);
        wrapper.ne("pay_status", 2);
        wrapper.le("overdue_time", new Date());
        List<LogisticsBillDTO> list = logisticsBillService.selectList(wrapper);
        if (list != null && list.size() > 0) {
            for (LogisticsBillDTO billDTO : list) {
                try {
                    logisticsBillService.saveOverdue(billDTO);
                } catch (ServiceException e){
                    log.error("逾期账单异常ServiceException: " + billDTO.getId(), e);
                } catch (RuntimeException e){
                    log.error("逾期账单RuntimeException: " + billDTO.getId(), e);
                } catch (Exception e){
                    log.error("逾期账单Exception: " + billDTO.getId(), e);
                }

            }
        }
    }
}
